<form class="layui-form seller-form" action="">
    <div class="layui-form-item">
        <div class="layui-inline" style="padding-left: 5px;">
            <select name="user_status" >
                <option value="">Please User Status</option>
                <option value="1">正常</option>
                <option value="2">停用</option>
            </select>
        </div>
        <div class="layui-inline">
            <input type="text" name="username" lay-verify="title" style="width:200px;"
                   class="layui-input" placeholder="Username">
        </div>
        <div class="layui-inline">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-sm" lay-submit lay-filter="*"><i class="iconfont icon-chaxun"></i>Search</button>
            </div>
        </div>
    </div>
</form>
<div class="table-body">
    <table id="balanceTable" lay-filter="balanceTable"></table>
</div>
<script type="text/html" id="toolbar">
    <button type="button" class="layui-btn layui-btn-sm" lay-event="ice">冻结用户</button>
    <button type="button" class="layui-btn layui-btn-sm" lay-event="active">激活用户</button>
</script>
<script>
    layui.use(['form', 'table'], function () {
        var table   = layui.table;
        var form    = layui.form;

        var tableIn =layui.table.render({
            elem: '#balanceTable',
            height: 'full-99',
            cellMinWidth: '80',
            url: "{:url('userFundRate')}",
            id: 'balanceTable',
            toolbar: '#toolbar',
            totalRow: true,
            cols: [[
                { type: 'checkbox', fixed: 'left'},
                { type: 'numbers'},
                { field: 'username', align: 'left', title: '用户' ,width:200,totalRowText:'Total：'},
                { field: 'status', title: '状态', width:100 ,
                    templet:function (d) {
                        const status=   d.status;
                        var   html  =   '';
                        switch (status) {
                            case 1:
                                html = "<span style='color: green'>正常</span>";
                                break;
                            case 2:
                                html = "<span style='color: #ff0000'>停用</span>";
                                break;
                        }
                        return html;
                    }
                },
                { field: 'active_card', align: 'right', title: '激活的卡' },
                { field: 'fc_amount', align: 'right',  title: '充值金额($)',
                    style:'color:#FF5722;font-weight:bold',totalRow: true},
                { field: 'dec_amount', align: 'right',  title: '拒付金额($)',
                    style:'color:#FF5722;font-weight:bold',totalRow: true},
                { field: 'amount_rate', title: '充值/拒付', align:"right"},
            ]],
            page: true,
            limit: 1000,
            limits: [1000],
        });

        form.on('submit(*)', function (data) {
            layui.table.reload('balanceTable', {
                where: data.field
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        table.on('toolbar(balanceTable)', function(obj) {
            const checkStatus = table.checkStatus(obj.config.id);
            const data = checkStatus.data;
            if (obj.event == 'ice') {
                if (data.length > 0) {
                    var id_array = [];
                    for (var i = 0; i < data.length; i++) {
                        if (data[i].status == 1) {
                            id_array.push(data[i].user_id);
                        }
                    }
                    if (id_array.length > 0) {
                        if (id_array.length > 20) {
                            layer.msg("每次只能选择20个用户.", {time: 2000, icon: 16});
                            return false;
                        }
                    } else {
                        layer.msg("至少选择一个未冻结用户", {time: 2000, icon: 16});
                        return false;
                    }

                    var loading = layer.load(1, {shade: [0.1, '#fff']});
                    $.post("{:url('user/userIce')}", {id_array: id_array}, function (res) {
                        layer.close(loading);
                        if (res.code > 0) {
                            layer.msg(res.msg, {time: 1500, icon: 1});
                            setTimeout("window.location.href='" + "{:url('user/userFundRate')}" + "'; ", 3000);
                        } else {
                            layer.msg(res.msg, {time: 1500, icon: 2});
                        }
                    });
                } else {
                    layer.msg("请选择需要冻结的用户.", {time: 2000, icon: 2});
                }
            }else if(obj.event == 'active' ){
                if (data.length > 0) {
                    var id_array = [];
                    for (var i = 0; i < data.length; i++) {
                        if (data[i].status == 2) {
                            id_array.push(data[i].user_id);
                        }
                    }
                    if (id_array.length > 0) {
                        if (id_array.length > 20) {
                            layer.msg("每次只能选择20个用户.", {time: 2000, icon: 16});
                            return false;
                        }
                    } else {
                        layer.msg("至少选择一个未激活用户", {time: 2000, icon: 16});
                        return false;
                    }

                    var loading = layer.load(1, {shade: [0.1, '#fff']});
                    $.post("{:url('user/userActive')}", {id_array: id_array}, function (res) {
                        layer.close(loading);
                        if (res.code > 0) {
                            layer.msg(res.msg, {time: 1500, icon: 1});
                            setTimeout("window.location.href='" + "{:url('user/userFundRate')}" + "'; ", 1500);
                        } else {
                            layer.msg(res.msg, {time: 1500, icon: 2});
                        }
                    });
                } else {
                    layer.msg("请选择需要激活的用户.", {time: 2000, icon: 2});
                }
            }
        });
    });
</script>